package com.alogic.remote.xscript.response;

import com.alogic.remote.Response;
import com.alogic.remote.xscript.ResponseHandler;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * 获取content的长度
 *
 * @since 1.6.13.6 [20200629 duanyy] <br>
 */
public class GetContentLength extends ResponseHandler {

    protected String id = "$http-content-length";

    public GetContentLength(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);

        id = PropertiesConstants.getRaw(p,"id",id);
    }

    @Override
    protected void onExecute(final Response res,
                             final XsObject root, final XsObject current, final LogicletContext ctx,
                             final ExecuteWatcher watcher) {
        String out = ctx.transform(id);
        if (StringUtils.isNotEmpty(out)){
            ctx.SetValue(out, String.valueOf(res.getContentLength()));
        }
    }
}